<template>
  <div class="com-container" ref="seller_ref">
    <div class="com-chart"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      currentPage: 1, //当前显示的页数
      totalPage: 0, //一共页数
      timerId: null
    };
  },

  created() {
    this.$socket.registerCallBack("sellerData", this.getData);
  },

  mounted() {
    this.initChart();
    this.$socket.send({
      action: "getData",
      socketType: "sellerData",
      chartName: "seller",
      value: ""
    });
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  computed: {
    ...mapState(["theme"])
  },
  watch: {
    theme() {
      console.log("主题切换了");
      this.chartInstance.dispose(); // 销毁当前的图表
      this.initChart(); // 重新以最新的主题名称初始化图表对象
      this.screenAdapter(); // 完成屏幕的适配
      this.updateChart(); // 更新图表的展示
    }
  },
  destroyed() {
    clearInterval(this.timerId);
    window.removeEventListener("resize", this.screenAdapter);
    this.$socket.unRegisterCallBack("sellerData");
  },

  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(
        this.$refs.seller_ref,
        this.theme
      );
      //对图表初始化配置的控制
      const initOption = {
        //标题
        title: {
          text: "▎商家销售统计",
          left: 20,
          top: 20
        },
        //坐标轴位置
        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true //距离包含坐标轴上的文字
        },
        xAxis: {
          type: "value"
        },
        yAxis: {
          type: "category"
        },
        tooltip: {
          //背景
          trigger: "axis",
          axisPointer: {
            type: "line",
            z: 0,
            lineStyle: {
              color: "#2D3443"
            }
          }
        },
        series: [
          {
            type: "bar",
            itemStyle: {
              //柱颜色渐变
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0,
                  color: "#5052EE"
                },
                {
                  offset: 1,
                  color: "#AB6EE5"
                }
              ])
            },
            label: {
              //柱文字
              show: true,
              position: "right",
              textStyle: {
                color: "white"
              }
            }
          }
        ]
      };
      this.chartInstance.setOption(initOption);

      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    getData(res) {
      //也可以利用Promise()拿到返回数据
      // this.$http.get("seller").then(response => {
      //   if (response.data) {
      //     console.log("sss");
      //   }
      // });

      // const { data: res } = await this.$http.get("seller");
      this.allData = res;
      this.allData.sort((o1, o2) => {
        return o1.value - o2.value;
      });

      //每5个显示一页
      this.totalPage =
        this.allData.length % 5 === 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1;
      this.updateChart();
      this.startInterval();
    },
    //更新图表
    updateChart() {
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const showData = this.allData.slice(start, end);

      //处理数据并更新界面图表
      const sellerNames = showData.map(item => {
        return item.name;
      });
      const sellerValues = showData.map(item => {
        return item.value;
      });
      const dataOption = {
        yAxis: {
          data: sellerNames
        },
        series: [
          {
            data: sellerValues
          }
        ]
      };
      this.chartInstance.setOption(dataOption);
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1;
        }
        this.updateChart();
      }, 3000);
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;

      //和分辨率大小有关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize
            }
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0]
            }
          }
        ]
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    }
  }
};
</script>
<style lang="less" scoped></style>
